package com.ruoyi.edu.controller.front;

/**
 * @author Alex
 * @version 1.0
 * @date 2023/9/23 0:52
 */

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.edu.api.domain.EduCourse;
import com.ruoyi.edu.api.domain.vo.CourseQueryVO;
import com.ruoyi.edu.api.domain.vo.EduChapterVO;
import com.ruoyi.edu.api.domain.vo.EduCourseVO;
import com.ruoyi.edu.service.IEduChapterService;
import com.ruoyi.edu.service.IEduCourseService;
import com.ruoyi.edu.service.IEduSubjectService;
import com.ruoyi.edu.service.IEduTeacherService;
import com.ruoyi.order.api.feign.IOrderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台教师模块
 *
 * @author isahino
 * @version 1.0
 * @date 2023/9/22 23:57
 */
@RestController
@RequestMapping("/front_course")
public class FrontEduCourseController {

    @Autowired
    private IEduChapterService eduChapterService;
    @Autowired
    private IEduCourseService eduCourseService;
    @Autowired
    private IEduSubjectService eduSubjectService;
    @Autowired
    private IEduTeacherService eduTeacherService;
    @Resource
    private IOrderClient iOrderClient;

    /**
     * 查询课程列表
     */
    @PostMapping("/list/{page}/{limit}")
    public AjaxResult list(@PathVariable Integer page, @PathVariable Integer limit,
                           @RequestBody(required = false) CourseQueryVO courseQueryVo) {
        Page<EduCourse> result = new Page<>(page, limit);
        eduCourseService.queryList(result, courseQueryVo);
        return AjaxResult.success(result);
    }

    /**
     * 查询所有课程分类
     */
    @GetMapping("/subject")
    public AjaxResult subjectList() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("list", eduSubjectService.getList());
        return AjaxResult.success(map);
    }

    /**
     * 查询课程详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult getCourseFrontInfo(@PathVariable String id, HttpServletRequest request) {
        boolean isBuy = false;
        //根据id查询课程信息
        EduCourseVO courseVO = eduCourseService.selectEduCourseById(id);
        if (courseVO.getPrice().compareTo(BigDecimal.ZERO) != 0) {
            // 课程不免费时，需要结合会员状态判断是否已购买
            // 会员处于登录态时查询课程是否已经购买
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            if (!"".equals(memberId)) {
                AjaxResult ajaxResult = iOrderClient.queryBuy(memberId, id);
                isBuy = JSON.parseObject(JSON.toJSONString(ajaxResult.get(AjaxResult.DATA_TAG)), Boolean.class);
            }
        } else {
            // 免费就直接视为已购买
            isBuy = true;
        }
        //比后台多封装一个教师头像
        courseVO.setAvatar(eduTeacherService.getById(courseVO.getTeacherId()).getAvatar());
        //根据课程id,查询章节和小节
        List<EduChapterVO> chapterVideoList = eduChapterService.getChapterVideo(id);
        Map<String, Object> map = new HashMap<>(3);
        map.put("isBuy", isBuy);
        map.put("course", courseVO);
        map.put("chapterVideoList", chapterVideoList);
        return AjaxResult.success(map);
    }

}
